package com.xyhuo.leetcode;

public class ReverseBits {

    public static void main(String[] args) {
        ReverseBits r = new ReverseBits();
        r.reverseBits(-3);
    }

    public int reverseBits(int n) {
        String binaryString = Integer.toBinaryString(n);
        char[] charArray = binaryString.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (int i = charArray.length - 1; i >= 0; i--) {
            sb.append(charArray[i]);
        }
        int length = charArray.length;
        while (length < 32) {
            length++;
            sb.append("0");
        }
        int result = Integer.parseUnsignedInt(sb.toString(), 2);
        return result;
    }
}
